package com.fayuan.nocv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fayuan.nocv.entity.BanJi;
import com.fayuan.nocv.entity.Department;
import com.fayuan.nocv.service.DepartmentService;
import com.fayuan.nocv.vo.BanjiVo;
import com.fayuan.nocv.vo.DataView;
import com.fayuan.nocv.vo.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/listDepartment")
    @ResponseBody
    public DataView listBanji(DepartmentVo departmentVo){

        QueryWrapper<Department>wrapper = new QueryWrapper<>();
        if (departmentVo.getName()!=null){
            wrapper.like("name",departmentVo.getName());
        }
        IPage<Department>page = new Page<>();
        IPage<Department> page1 = departmentService.page(page, wrapper);
        return new DataView(page1.getTotal(),page1.getRecords());
    }

    @PostMapping("/addOrUpdateDepartment")
    @ResponseBody
    public DataView addOrUpdateDepartment(Department department){

        departmentService.saveOrUpdate(department);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("科室操作成功");
        return  dataView;
    }

    @PostMapping("/deleteById")
    @ResponseBody
    public DataView DeleteDepartment(Department department){

        departmentService.removeById(department.getId());
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("班级删除成功");
        return  dataView;
    }
}
